import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QCheckBox, QPushButton, QMessageBox, QLabel, QScrollArea

class AudioFormatSelector(QWidget):
    def __init__(self):
        super().__init__()
        self.type_elected_formats = None
        self.initUI()

    def initUI(self):
        self.layout = QVBoxLayout(self)
        self.label1 = QLabel('选择输出格式')
        self.layout.addWidget(self.label1)
        self.label1.setAlignment(Qt.AlignCenter)

        self.boxes = {}
        music_formats = [
            "MP3", "WAV", "OGG", "WMA", "AAC", "AIFF", "M4A",
            "FLAC", "ALAC", "APE", "AMR", "MIDI", "MKA", "MP2",
            "OFR", "RA", "RM", "SHN", "TTA", "WV", "WEBM"
        ]

        self.scroll_area = QScrollArea()
        self.scroll_area.setWidgetResizable(True)
        self.scroll_content = QWidget()
        scroll_layout = QVBoxLayout(self.scroll_content)

        for format in music_formats:
            checkbox = QCheckBox(f"选择 {format}")
            scroll_layout.addWidget(checkbox)
            self.boxes[format.lower()] = checkbox
            # 默认选中前三个复选框
            if format in ["MP3", "WAV", "OGG"]:
                checkbox.setChecked(True)

        self.scroll_area.setWidget(self.scroll_content)
        self.layout.addWidget(self.scroll_area)

        self.setWindowTitle('音频格式选择器')
        self.resize(300, 400)

        # # 创建一个按钮，用于显示选中的格式
        # self.showSelectedButton = QPushButton('显示选中的格式')
        # self.showSelectedButton.clicked.connect(self.showSelectedFormats)
        # self.layout.addWidget(self.showSelectedButton)

    def showSelectedFormats(self):
        selected_formats = [format for format, checkbox in self.boxes.items() if checkbox.isChecked()]
        if selected_formats:
            self.type_elected_formats = selected_formats
            QMessageBox.information(self, "选中的格式", ", ".join(selected_formats))
            print(self.type_elected_formats)
        else:
            QMessageBox.information(self, "选中的格式", "没有格式被选中")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    extractor = AudioFormatSelector()
    extractor.show()
    sys.exit(app.exec_())